/* Owner.m
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * 
 *
 * Written by: Mai Nguyen, NeXT Developer Support
 */


#import <eointerface/eointerface.h>
#import "Owner.h"
#import "Author.h"

#define ARCHIVE_FILE	"ObjectArchive"

@implementation Owner

 
- appDidInit:sender
{
	EOAdaptorChannel	*adaptorChannel;
	
   	/* set up the adaptor channel for debugging */
    adaptorChannel = [[dataSource databaseChannel] adaptorChannel]; 
    [(EOAdaptorChannel *)adaptorChannel setDelegate: self];
    return self;
}

    
- (void)adaptorChannel:channel didEvaluateExpression:(NSString *)expression
{
    NSLog (expression);
}

/* 	Archive the first object in the array of selected objects.
 *  In this example, only one object is selected at a time.
 */
- writeObject:sender
{
	eoArray = [controller selectedObjects];
    // Archive the first object in the array of selected objects.
    //  In this example, only one object is selected at a time.
	if ([eoArray count] == 0) {
		NXRunAlertPanel(NULL, "No object to archive!", NULL, NULL, NULL);
		return self;
		}
		
	eoAuthor = [eoArray objectAtIndex:0];
	[self archiveThisObject:eoAuthor]; 
	
	return self;
}

- readObject:sender
{
	eoAuthor = (Author *)[self unarchiveObject];
	return self;
}


- (BOOL) archiveThisObject:(Object *)object
{
    NXTypedStream *stream;
	char	archivePath[MAXPATHLEN+1];
    BOOL b = YES;
    
    if (!object)
		return NO;
        
    NS_DURING
    
	sprintf(archivePath, "%s/%s", [[self applicationPath] cString], ARCHIVE_FILE);
 	stream = NXOpenTypedStreamForFile(archivePath, NX_WRITEONLY);
    NXWriteRootObject(stream, object);
    NXCloseTypedStream(stream);

    NS_HANDLER
    
	[textObject appendText:"Object archiving failed\n"];
    b = NO;
    
    NS_ENDHANDLER
    
    if (b == YES)   
   		[textObject appendText:"Object archiving succeeded\n"];
	   
    return b;
}

-(Object *)unarchiveObject 
{
    NXTypedStream *stream;
    Object *object;
    char archivePath[MAXPATHLEN+1];
    char buf[1024];
        
   
    sprintf(archivePath, "%s/%s", [[self applicationPath] cString], ARCHIVE_FILE);
    stream = NXOpenTypedStreamForFile(archivePath, NX_READONLY);
    if (!stream) {
    	object = nil;
	[textObject appendText:"Object unarchiving failed\n"];
	return object;
    }
        
    object = NXReadObject(stream);
 
    NXCloseTypedStream(stream);
    [textObject appendText:"Object unarchiving succeeded\n"]; 
    sprintf(buf, "%s\n",[[(Author *)object description] cString]);
    [textObject appendText: "Author contents:"];
    [textObject appendText:buf];
	     	
    return [object autorelease];
}

- (NSString *)applicationPath 
{
	int           i;
	NSString   *appPathName = nil;

	for (i = strlen(NXArgv[0]) - 1; (i >= 0) && (NXArgv[0][i] != '/'); i--);
	appPathName = [NSString stringWithCString:NXArgv[0] length:i];
	if (NXArgv[0][0] != '/') {
		char path[MAXPATHLEN+1];
		appPathName = [NSString stringWithFormat:@"%s/%@", getwd(path), appPathName];
	}
	return appPathName;
}

@end


@implementation Text(printResults)

- appendText:(const char *)newText
{
	int currentLength = [self textLength];

	[self setSel:currentLength :currentLength];
	[self replaceSel:newText];
	[self scrollSelToVisible];
	return self;
}

@end
